package com.wan.core.timer.round;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;

/**
 * 现实自然的时间轮规则
 * @author wwh
 * @date 2022/6/27 20:33
 */
public enum NatureRound implements IRoundWheel {
    //周轮，一周七天
    DAY_OF_WEEK(null,7){
        @Override
        public int calculateCurrentBucket(LocalDateTime localDateTime, Duration duration) {
            DayOfWeek dayOfWeek = DayOfWeek.from(localDateTime);
            return dayOfWeek.ordinal();
        }
    },
    //时轮，24小时->一天
    HOUR(DAY_OF_WEEK,24){
        @Override
        public int calculateCurrentBucket(LocalDateTime localDateTime, Duration duration) {
            return localDateTime.getHour();
        }
    },
    //分轮，60分->一小时
    MINUTE(HOUR,60){
        @Override
        public int calculateCurrentBucket(LocalDateTime localDateTime, Duration duration) {
            return localDateTime.getMinute();
        }
    },
    //秒轮，60秒->一分钟
    SECOND(MINUTE,60){
        @Override
        public int calculateCurrentBucket(LocalDateTime localDateTime, Duration duration) {
            return localDateTime.getSecond();
        }
    },
    ;

    private final IRoundWheel parent;
    private final int bucketNumber;

    NatureRound(IRoundWheel parent, int bucketNumber) {
        this.parent = parent;
        this.bucketNumber = bucketNumber;
    }

    @Override
    public IRoundWheel getParent() {
        return parent;
    }

    @Override
    public int getBucketNumber() {
        return bucketNumber;
    }
}
